package com.bank.gui;

import com.bank.entity.Branch;
import com.bank.entity.MySQLConnection;
import com.bank.utils.CommunicationWrapper;
import com.bank.utils.Operation;
import com.bank.utils.TextFieldLimiter;
import com.bank.utils.Toast;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.text.AbstractDocument;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Moofie
 */
public class RegisterPanel extends javax.swing.JPanel {

    private boolean validate;
    private CommunicationWrapper cw;
    private String branchCode;
    private int portNo;

    /**
     * Creates new form RegisterPanel
     */
    public RegisterPanel(CommunicationWrapper cw, String branchCode, int portNo) {
        this.cw = cw;
        this.branchCode = branchCode;
        this.portNo = portNo;
        initComponents();
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btngrp = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        txtFname = new javax.swing.JTextField();
        txtLname = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jrbMale = new javax.swing.JRadioButton();
        jrbFemale = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jcbState = new javax.swing.JComboBox();
        txtCity = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtICNumber = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtContact = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        btnSubmit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jcbAccType = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Firstname");
        add(jLabel1);
        jLabel1.setBounds(20, 30, 150, 30);

        AbstractDocument aDocname = (AbstractDocument)txtFname.getDocument();
        aDocname.setDocumentFilter(new TextFieldLimiter("^[a-z A-Z]+$"));
        txtFname.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtFname);
        txtFname.setBounds(170, 30, 250, 30);

        AbstractDocument aDocLname = (AbstractDocument)txtLname.getDocument();
        aDocLname.setDocumentFilter(new TextFieldLimiter("^[a-zA-Z]+$"));
        txtLname.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtLname);
        txtLname.setBounds(170, 60, 250, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Lastname");
        add(jLabel2);
        jLabel2.setBounds(20, 60, 150, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Gender");
        add(jLabel3);
        jLabel3.setBounds(20, 120, 150, 30);

        txtAddress.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtAddress);
        txtAddress.setBounds(170, 150, 250, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Address");
        add(jLabel4);
        jLabel4.setBounds(20, 150, 150, 30);

        btngrp.add(jrbMale);
        jrbMale.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMale.setSelected(true);
        jrbMale.setText("Male");
        add(jrbMale);
        jrbMale.setBounds(220, 120, 70, 30);

        btngrp.add(jrbFemale);
        jrbFemale.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbFemale.setText("Female");
        add(jrbFemale);
        jrbFemale.setBounds(320, 120, 80, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("State");
        add(jLabel5);
        jLabel5.setBounds(20, 180, 150, 30);

        jcbState.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jcbState.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Perak", "Selangor", "Seremban", "Melaka", "Wilayah Persekutuan" }));
        add(jcbState);
        jcbState.setBounds(170, 180, 250, 30);

        AbstractDocument aDocCity = (AbstractDocument)txtCity.getDocument();
        aDocCity.setDocumentFilter(new TextFieldLimiter("^[a-z A-Z]+$"));
        txtCity.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtCity);
        txtCity.setBounds(170, 210, 250, 30);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("City");
        add(jLabel6);
        jLabel6.setBounds(20, 210, 150, 30);

        AbstractDocument aDocPostCode = (AbstractDocument)txtICNumber.getDocument();
        aDocPostCode.setDocumentFilter(new TextFieldLimiter("\\d{0,12}"));
        txtICNumber.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtICNumber);
        txtICNumber.setBounds(170, 90, 250, 30);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("IC Number");
        add(jLabel7);
        jLabel7.setBounds(20, 90, 150, 30);

        AbstractDocument aDocContact = (AbstractDocument)txtContact.getDocument();
        aDocContact.setDocumentFilter(new TextFieldLimiter("\\d{0,10}"));
        txtContact.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtContact);
        txtContact.setBounds(170, 240, 250, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Contact Number");
        add(jLabel8);
        jLabel8.setBounds(20, 240, 150, 30);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Email");
        add(jLabel9);
        jLabel9.setBounds(20, 270, 150, 30);

        txtEmail.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtEmail);
        txtEmail.setBounds(170, 270, 250, 30);

        btnSubmit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });
        add(btnSubmit);
        btnSubmit.setBounds(260, 340, 160, 30);

        btnReset.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        add(btnReset);
        btnReset.setBounds(20, 340, 160, 30);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Account Type");
        add(jLabel10);
        jLabel10.setBounds(20, 300, 150, 30);

        jcbAccType.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jcbAccType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Saving", "Current" }));
        add(jcbAccType);
        jcbAccType.setBounds(170, 300, 250, 30);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("ACCOUNT REGISTRATION");
        add(jLabel11);
        jLabel11.setBounds(20, 5, 400, 20);
        add(jSeparator1);
        jSeparator1.setBounds(10, 25, 420, 5);
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        clear();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        // prepare string for error message
        StringBuilder check = new StringBuilder("<html>Errors");
        validate = true;

        if (txtFname.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter first name");
        }

        if (txtLname.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter last name");
        }

        if (txtAddress.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter address");
        }
        if (txtCity.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter city");
        }
        if (txtICNumber.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter IC number");
        }

        if (txtContact.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter contact number");
        }
        if (txtEmail.getText().length() == 0) {
            validate = false;
            check.append("<br />Please enter email");
        }

        if (!validate) {
            // validation failed, display error message
            check.append("</html>");
            Toast.makeText(getParent(), 150, "" + check, Toast.LENGTH_LONG).display();
        } else {
            // validation passed, proceed to constructing data
            try {
                String gender;
                JSONObject j = new JSONObject();
                // specific operation
                j.put("operation", Operation.REGISTER);
                JSONObject content = new JSONObject();
                // put in all necessay data
                content.put("firstname", txtFname.getText());
                content.put("lastname", txtLname.getText().trim());
                content.put("icno", txtICNumber.getText().trim());
                if (jrbMale.isSelected()) {
                    gender = "M";
                } else {
                    gender = "F";
                }
                Branch b = new Branch();
                b.setBranchCode(this.branchCode);
                content.put("gender", gender);
                content.put("addressss", txtAddress.getText());
                content.put("state", jcbState.getSelectedItem().toString());
                content.put("city", txtCity.getText());
                content.put("postcode", postcode(jcbState.getSelectedIndex()));
                content.put("contact", txtContact.getText().trim());
                content.put("email", txtEmail.getText().trim());
                content.put("type", jcbAccType.getSelectedItem().toString());
                content.put("bid", Integer.parseInt(b.obtainBranchId(MySQLConnection.getConnection())));
                content.put("port", this.portNo);
                content.put("bCode", this.branchCode);
                content.put("address", InetAddress.getLocalHost().getHostAddress());
                j.put("content", content);
                // after finished constructing data, send to server
                cw.send(j, InetAddress.getLocalHost(), 5000);
                // wait for server's reply
                JSONObject js = cw.receive();
                if (js.getString("result").equalsIgnoreCase("Success")) {
                    // operation succeed, display successful message
                    Toast.makeText(getParent(), "Registration successful", Toast.LENGTH_SHORT).display();
                } else {
                    // operation failed, display error message
                    Toast.makeText(getParent(), "Unable to register now, try again later", Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnSubmitActionPerformed
    
    /**
     * generate post code based on selected state
     * 
     * @param index selected index
     * @return post code
     */
    
    private String postcode(int index) {
        String postcode;
        switch (index) {
            case 0:
                postcode = "32000";
                return postcode;
            case 1:
                postcode = "63300";
                return postcode;
            case 2:
                postcode = "77890";
                return postcode;
            case 3:
                postcode = "44690";
                return postcode;
            case 4:
                postcode = "53200";
                return postcode;
        }
        return "Not available";


    }
    
    /**
     * Clear all field
     */

    public void clear() {
        txtFname.setText("");
        txtLname.setText("");
        jrbMale.setSelected(true);
        txtAddress.setText("");
        jcbState.setSelectedIndex(0);
        txtCity.setText("");
        txtICNumber.setText("");
        txtContact.setText("");
        txtEmail.setText("");
        jcbAccType.setSelectedIndex(0);

    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.ButtonGroup btngrp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JComboBox jcbAccType;
    private javax.swing.JComboBox jcbState;
    private javax.swing.JRadioButton jrbFemale;
    private javax.swing.JRadioButton jrbMale;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtCity;
    private javax.swing.JTextField txtContact;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFname;
    private javax.swing.JTextField txtICNumber;
    private javax.swing.JTextField txtLname;
    // End of variables declaration//GEN-END:variables
}
